package com.wy.controller;

import com.wy.model.User;
import com.wy.utils.QueryResult;
import com.wy.utils.Result;
import com.wy.service.UserService;
import com.wy.utils.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;

@Api(value="用户Scontroller",tags={"用户操作接口"})
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 模糊分页查询
     * @param user
     * @return
     */
    @ApiOperation(value="获取列表")
    @PostMapping("list")
    public QueryResult<User> list(@RequestBody  @ApiParam(name="用户对象",value="传入json格式",required=false) User user){
        log.debug("--进入列表查询list方法--");
        PageHelper.startPage(user.getRow(),user.getSize());
        List<User> list = userService.list(user);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        return new QueryResult<User>(pageInfo.getList(),pageInfo.getTotal());
    }


    /**
     * 新增/修改
     * @param user
     * @return
     */
    @ApiOperation(value="新增/修改")
    @PostMapping("save")
    public Result save(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) User user){
        String id = user.getId();
        if(StringUtils.isEmpty(id)){
            userService.insert(user);
        }  else{
            userService.update(user);
        }
        return Result.resultOk(user);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value="根据id查询")
    @ApiImplicitParam(name="id",value="用户id",dataType="String", paramType = "query",required = true)
    @GetMapping("getById/{id}")
    public User getById(@PathVariable String id){
        return userService.getById(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value="批量删除")
    @ApiImplicitParam(name="ids",value="试卷id数组",dataType="String[]", paramType = "query",required = true)
    @PostMapping("deletebth")
    public Result deletebth(@RequestBody String[] ids){
        int res = userService.deletebth(ids);
        return Result.resultOk(res);
    }
}
